package com.orhon.smartcampus.modules.student.fetcher;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.student.dao.ParentsDao;
import com.orhon.smartcampus.modules.student.entity.Parents;
import com.orhon.smartcampus.modules.student.gqlModel.GQLParents;
import com.orhon.smartcampus.modules.student.mm.ParentsMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;


@Component
public class ParentsFetcher implements DataFetcher {
    @Autowired
    ParentsDao parentsDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
   
        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
        String hql = "from Parents where deleted_at = null ";
        if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("deleted_at")!=null) {
				hql += " and deleted_at  " + Util.decide(map.get("deleted_at"));
			}
			if(map.get("id")!=null) {
				hql += " and id  " + Util.decide(map.get("id"));
			}
			if(map.get("studentId")!=null) {
				hql += " and student_id  " + Util.decide(map.get("studentId"));
			}
			hql += " order by id desc";
		}
        List<Parents> list = parentsDao.find(hql,null,page.getPage(),page.getLimit());
        List<GQLParents> extendsList = ParentsMapper.MAPPER.toParentsList(list);
        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
        	 total = parentsDao.getTotalCount(hql);
        }
        return Result.ok().put("data", extendsList).put("total", total);
    }


}
